import request from '@/utils/request'

/**
 * 获取分类树
 * @param {string} type - 分类类型
 * @returns {Promise} 返回分类树数据
 */
export function getCategoryTree(type) {
    return request({
        url: 'category/api/category/tree',
        method: 'get',
        params: {
            type
        }
    })
}

/**
 * 添加分类
 * @param {Object} data - 分类数据
 * @param {string} data.name - 分类名称
 * @param {string} data.code - 分类编码
 * @param {string} [data.parentId] - 父级分类ID
 * @param {number} [data.sort] - 排序
 * @param {string} [data.icon] - 图标
 * @param {string} [data.description] - 描述
 * @param {string} data.type - 分类类型
 * @returns {Promise}
 */
export function addCategory(data) {
    return request({
        url: 'category/api/category',
        method: 'post',
        data
    })
}

// 获取分类列表（支持查询）
export function listCategory(query) {
    return request({
        url: '/category/api/category/list',
        method: 'get',
        params: query
    })
}

// 获取分类详情
export function getCategory(id) {
    return request({
        url: `/category/api/category/${id}`,
        method: 'get'
    })
}

// 更新分类
export function updateCategory(data) {
    return request({
        url: `/category/api/category/${data.id}`,
        method: 'put',
        data
    })
}

// 删除分类
export function deleteCategory(id) {
    return request({
        url: `/category/api/category/${id}`,
        method: 'delete'
    })
}

// 批量删除分类
export function batchDeleteCategory(ids) {
    return request({
        url: '/category/api/category/batch',
        method: 'delete',
        data: {
            ids: ids
        }
    })
}

// 获取指定分类的子分类
export function getChildren(id) {
    return request({
        url: `/category/api/category/${id}/children`,
        method: 'get'
    })
}

// 获取分类路径
export function getCategoryPath(id) {
    return request({
        url: `/category/api/category/${id}/path`,
        method: 'get'
    })
}

// 更新分类状态
export function updateCategoryStatus(id, status) {
    return request({
        url: `/category/api/category/${id}/status`,
        method: 'put',
        data: { status }
    })
}

// 更新分类排序
export function updateCategorySort(id, sort) {
    return request({
        url: `/category/api/category/${id}/sort`,
        method: 'put',
        data: { sort }
    })
}